from random import random

from django.conf import settings
from django.http import HttpResponse

from utils.tencent.sms import send_sms_single


# Create your views here.
def send_sms(request):
    """
        发送短信
        ?tpl=login -> 548762
        ?tpl=register -> 548760
    """
    tpl = request.GET.get('tpl')
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)

    if not template_id:
        return HttpResponse("模板不存在！")

    code = random.randint(1000, 9999)
    res = send_sms_single("13780759435", 548760, [code,])
    if res['result'] == 0:
        return HttpResponse("成功！")
    else:
        return HttpResponse(res['message'])